import os
import datetime


class Logger:
    def __init__(self, filename=None):
        if filename is None:
            current_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
            filename = f"log_{current_time}.txt"
        logs_directory = 'logs'
        if not os.path.exists(logs_directory):
            os.makedirs(logs_directory)
        self.filename = os.path.join(logs_directory, filename)

    def log(self, message):
        timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        log_message = f"[{timestamp}] {message}\n"
        with open(self.filename, 'a', encoding='utf-8') as file:  # 指定编码为 UTF-8
            file.write(log_message)
        print(log_message.strip())

    def error(self, message):
        self.log(message + "=====================error=============")


logger = Logger()  # 使用默认的日志文件名

# 示例用法
if __name__ == "__main__":
    logger.log("这是一条日志信息。")
    logger.log("这是另一条日志信息。")
